package com.haierCamera.customapplication.ui.main.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.haierCamera.customapplication.R;

import java.util.Calendar;

public class DialogEditDevice extends Dialog {
    private Context context;
    private OnSelectedListener listener;

    private View view;

    private ImageView iv_msg;
    private ImageView iv_record;
    private ImageView iv_detail;
    private ImageView iv_repair;

    public DialogEditDevice(Context context) {
        super(context);
        this.context = context;
        this.setCancelable(false);
        init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(R.color.HZKLtransparent);
        setCanceledOnTouchOutside(true);
        super.onCreate(savedInstanceState);

    }

    public void init() {
        Log.i("TAG","DialogWifiLinkState_init");
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.hzkl_dialog_device_edit, null);

        iv_msg = view.findViewById(R.id.iv_msg);
        iv_record = view.findViewById(R.id.iv_record);
        iv_detail = view.findViewById(R.id.iv_detail);
        iv_repair = view.findViewById(R.id.iv_repair);

        iv_msg.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                listener.click(0);
                dismiss();
            }
        });

        iv_record.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                listener.click(1);
                dismiss();
            }
        });

        iv_detail.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                listener.click(2);
                dismiss();
            }
        });

        iv_repair.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                listener.click(3);
                dismiss();
            }
        });
    }

    public void showDialog(){
        show();
        setContentView(view,new LinearLayout.LayoutParams(//设置dialog
                (int) (context.getResources().getDisplayMetrics().widthPixels * 0.9),
                LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    public DialogEditDevice setListener(OnSelectedListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnSelectedListener {
        void click(int type);
    }

    //为了不让按钮重复点击
    public abstract class NoDoubleClickListener implements View.OnClickListener {
        public static final int MIN_CLICK_DELAY_TIME = 2500;//这里设置不能超过多长时间
        private long lastClickTime = 0;

        protected abstract void onNoDoubleClick(View v);

        @Override
        public void onClick(View v) {
            long currentTime = Calendar.getInstance().getTimeInMillis();
            if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                lastClickTime = currentTime;
                onNoDoubleClick(v);
            }
        }
    }

}
